package com.elock.dao;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.time.DateUtils;
import org.springframework.stereotype.Repository;

import com.elock.base.core.Page;
import com.elock.base.core.Pageable;
import com.elock.base.core.dao.impl.DaoCenter;
import com.elock.base.core.util.ConvertUtil;
import com.elock.base.core.util.DateUtil;
import com.elock.base.core.util.ExceptionUtil;
import com.elock.base.core.util.WebUtils;
import com.elock.entity.Member;

/**
 * Dao - 用户
 * @author 仁德
 */
@Repository("memberDao")
public class MemberDao extends DaoCenter {

//	@Resource(name = "saleOrgBaseServiceImpl")
//	private SaleOrgBaseService saleOrgBaseService;

	/**
	 * 根据用户名查找用户
	 */
	public Member findByUsername(String username, Long companyInfoId) {

		if (ConvertUtil.isEmpty(username)) {
			ExceptionUtil.throwDaoException("username is empty!");
		}

		StringBuilder sql = new StringBuilder();
		sql.append("select * from member where is_enabled = 1 and username = ?");
		Object[] objs = null;
		if (companyInfoId != null) {
			sql.append(" and company_info_id = ?");
			objs = new Object[] { username, companyInfoId };
		}
		else {
			sql.append(" and company_info_id is null");
			objs = new Object[] { username };
		}

		Member member = getNativeDao().findSingleManaged(sql.toString(),
				objs,
				Member.class);

		return member;
	}

	/**
	 * 根据手机号查找用户
	 * 
	 * @param username
	 * @return
	 */
	public Member findByMobile(String mobile, Long companyInfoId) {

		if (ConvertUtil.isEmpty(mobile)) {
			ExceptionUtil.throwDaoException("value is empty!");
		}
		Object[] objs = null;
		String sql = "select * from member where mobile = ?";
		if (companyInfoId != null) {
			sql += " and company_info_id = ?";
			objs = new Object[] { mobile, companyInfoId };
		}
		else {
			sql += " and company_info_id is null";
			objs = new Object[] { mobile };
		}
		Member member = getNativeDao().findSingleManaged(sql, objs, Member.class);
		return member;
	}

	public Member findByPinCode(String pinCode) {

		if (ConvertUtil.isEmpty(pinCode)) {
			ExceptionUtil.throwDaoException("value is empty!");
		}
		String sql = "select * from member where pin_code = ?";
		Member member = getNativeDao().findSingleManaged(sql,
				new Object[] { pinCode },
				Member.class);
		return member;
	}

	public Page<Map<String, Object>> findPage(String username, String mobile, String name,
			String pinCode, Long saleOrgId, String startTime, String endTime,
			Integer memberType, Pageable pageable) {

		List<Object> list = new ArrayList<Object>();
		StringBuilder sql = new StringBuilder();
		Long companyInfoId = WebUtils.getCurrentCompanyInfoId();
		sql.append(
				"select m.*,so.name sale_org_name from member m left join member_sale_org mso on m.id=mso.member");
		sql.append(" left join sale_org so on so.id=mso.sale_org where 1=1");
		if (companyInfoId != null) {
			sql.append(" and m.company_info_id=?");
			list.add(companyInfoId);
		}
		if (!ConvertUtil.isEmpty(username)) {
			sql.append(" and m.username like ?");
			list.add("%" + username + "%");
		}
		if (!ConvertUtil.isEmpty(mobile)) {
			sql.append(" and m.mobile like ?");
			list.add("%" + mobile + "%");
		}
		if (!ConvertUtil.isEmpty(name)) {
			sql.append(" and m.name like ?");
			list.add("%" + name + "%");
		}
		if (!ConvertUtil.isEmpty(pinCode)) {
			sql.append(" and m.pin_code like ?");
			list.add("%" + pinCode + "%");
		}
		if (saleOrgId != null) {
			sql.append(" and so.id=?");
			list.add(saleOrgId);
		}
		if (!ConvertUtil.isEmpty(startTime)) {
			sql.append(" and m.create_date >= ?");
			list.add(DateUtil.convert(startTime + " 00:00:00"));
		}
		if (!ConvertUtil.isEmpty(endTime)) {
			sql.append(" and m.create_date < ?");
			list.add(DateUtils.addDays(DateUtil.convert(endTime + " 00:00:00"), 1));
		}
		if (memberType != null) {
			sql.append(" and m.member_type=?");
			list.add(memberType);
		}
//		sql.append(" and so.id in (" + saleOrgBaseService.saleOrgAuth() + ")");
		sql.append(" order by m.create_date desc");

		Object[] objs = new Object[list.size()];
		for (int i = 0; i < list.size(); i++) {
			objs[i] = list.get(i);
		}

		Page<Map<String, Object>> getPage = getNativeDao().findPageMap(sql.toString(),
				objs,
				pageable);

		return getPage;

	}
}